<?php include 'header.php' ?>

    <div class="container_12 primary_content_wrap clearfix">
        <div id="content" class="grid_8 right" style="width: 670px;">
            <div class="shop">
                <div class="bc-wrap">
                    <div id="breadcrumb"><a href="index.php"
                                            class="home">Trang chủ</a> › <a
                            href="product.php">Sản phẩm</a></div>
                </div>
                <h1 class="page_title">Sản phẩm</h1>
                <ul class="products">
                    <?php
                    include 'mySQLConnect.php';

                    $type = $_GET['type'];
                    $additionSQL = "";

                    if ($type == "cate") {
                        $cat_id = $_GET['cat_id'];
                        $additionSQL = "WHERE cat_id = '$cat_id'";
                    } else if ($type == "price") {
                        $pStart = $_GET['pStart'];
                        $pEnd = $_GET['pEnd'];
                        if (isset($pEnd)) {
                            $additionSQL = "WHERE price >= '$pStart' AND price < '$pEnd'";
                        } else {
                            $additionSQL = "WHERE price >= '$pStart'";
                        }
                    }

                    //xac dinh bao nhieu dong
                    $display = 9;
                    // tinh tong so trang can hien thi
                    if (isset($_GET['page']) && (int)$_GET['page']) {
                        $page = $_GET['page'];

                    } else {
                        //neu chua xac dinh, thi tim so trang
                        $query = "SELECT COUNT(id) FROM product " . $additionSQL;
                        $res = mysql_query($query);
                        $rows = mysql_fetch_array($res) or die(mysql_error());
                        $record = $rows[0];
                        if ($record > $display) {
                            $page = ceil($record / $display);
                        } else {
                            $page = 1;
                        }
                    }

                    $start = (isset($_GET['start']) && (int)$_GET['start'] >= 0) ? $_GET['start'] : 0;

                    $sql3 = "SELECT * FROM  product " . $additionSQL . " ORDER BY date DESC LIMIT $start, $display";

                    $result3 = mysql_query($sql3);
                    $count = 0;
                    while ($row3 = mysql_fetch_array($result3)) {
                        $count++;
                        $image_url = $row3['image_url'];

                        if (strpos($image_url, '../') !== false) {
                            $image_url = substr($image_url, 3);
                        }

                        $price = ($row3['price']);
						$temp = "";
						
						$price = strrev($price);
                        while (strlen($price) >= 4) {							
                            $temp = $temp . substr ($price,0,3) . '.';
							$price = substr ($price,3);
                        }
						
						if (strlen($price) == 0){
							$price = strrev($temp);
						} else {
							$price = strrev($price) . strrev($temp);
						}
                        if ($count % 3 == 1) {
                            ?>
                            <li class="product first">
                                <div class="product-inner">
                                    <div class="product-image-wrap"><a class="product-image"
                                                                       href="detailProduct.php?id=<?php echo $row3['id'] ?>"><img
                                                width="305" height="305" alt="<?php echo urldecode($row3['name']) ?>"
                                                class="attachment-shop_small wp-post-image"
                                                src="<?php echo $image_url ?>"
                                                style="opacity: 1;"></a></div>
                                    <div class="caption">

                                        <h3 class="title">
                                            <a class="product-info"
                                               href="detailProduct.php?id=<?php echo $row3['id'] ?>">
                                                <?php echo urldecode($row3['name']) ?>
                                            </a>
                                        </h3>
                                        <span class="price"><?php echo $price ?> VND</span> <a class="button"
                                                                                               href="detailProduct.php?id=<?php echo $row3['id'] ?>">Chi
                                            tiết</a>
                                    </div>
                                </div>
                            </li>
                        <?php } else if ($count % 3 == 2) { ?>
                            <li class="product ">
                                <div class="product-inner">
                                    <div class="product-image-wrap"><a class="product-image"
                                                                       href="detailProduct.php?id=<?php echo $row3['id'] ?>"><img
                                                width="305" height="305" alt="<?php echo urldecode($row3['name']) ?>"
                                                class="attachment-shop_small wp-post-image"
                                                src="<?php echo $image_url ?>"
                                                style="opacity: 1;"></a></div>
                                    <div class="caption">
                                        <h3 class="title"><a class="product-info"
                                                             href="detailProduct.php?id=<?php echo $row3['id'] ?>"><?php echo urldecode($row3['name']) ?></a>
                                        </h3>
                                        <span class="price"><?php echo $price ?> VND</span> <a class="button"
                                                                                               href="detailProduct.php?id=<?php echo $row3['id'] ?>">Chi
                                            tiết</a>
                                    </div>
                                </div>
                            </li>
                        <?php } else { ?>
                            <li class="product last">
                                <div class="product-inner">
                                    <div class="product-image-wrap"><a class="product-image"
                                                                       href="detailProduct.php?id=<?php echo $row3['id'] ?>"><img
                                                width="305" height="305"
                                                alt="<?php echo urldecode($row3['name']) ?>"
                                                class="attachment-shop_small wp-post-image"
                                                src="<?php echo $image_url ?>"
                                                style="opacity: 1;"></a></div>
                                    <div class="caption">
                                        <h3 class="title"><a class="product-info"
                                                             href="detailProduct.php?id=<?php echo $row3['id'] ?>"><?php echo urldecode($row3['name']) ?></a>
                                        </h3>
                                        <span class="price"><?php echo $price ?> VND</span> <a class="button"
                                                                                               href="detailProduct.php?id=<?php echo $row3['id'] ?>">Chi
                                            tiết</a>
                                    </div>
                                </div>
                            </li>
                        <?php
                        }
                    }
                    if ($count == 0) {
                        echo "<li>sản phẩm đang được cập nhật</li>";
                    }

                    include 'mySQLClose.php';
                    ?>
                </ul>
                <div class="clear"></div>
                <div class="pagenavi">
                    <?php
                    if ($page > 1) { //neu can hien thi so trang
                        $next = $start + $display;
                        $prev = $start - $display;
                        $current = ($start / $display) + 1;

                        $datastrPrv = 'start=' . $prev . '&page=' . $page;
                        $datastrNext = "start=" . $next . "&page=" . $page;

                        //Hien thi trang Previous
                        if ($current != 1) {
                            ?>
                            <a class="pagenavi_prev" href="searchProduct.php?type=cate&cat_id=<?php echo $cat_id; ?>&<?php echo $datastrPrv; ?>">Previous</a>
                        <?php
                        }
                        //Hien thi so link
                        for ($i = 1; $i <= $page; $i++) {
                            if ($current != $i) {
                                $datastrPage = "start=" . ($display * ($i - 1)) . "&page=" . $page;
                                ?>
                                <a class="inactive" href="searchProduct.php?type=cate&cat_id=<?php echo $cat_id; ?>&<?php echo $datastrPage; ?>"><?php echo $i ?></a>
                            <?php
                            } else {
                                echo "<span class='current'>$i</span>";
                            }
                        } //End: FOR

                        //Hien thi trang Next

                        if ($current != $page) {
                            ?>
                            <a class="pagenavi_next" href="searchProduct.php?type=cate&cat_id=<?php echo $cat_id; ?>&<?php echo $datastrNext ?>">Next</a>
                        <?php
                        }
                    }
                    ?>
                </div>
            </div>
        </div>
        <aside class="grid_4" id="sidebar" style="width: 250px;">
            <?php include 'rightMenu.php'; ?>
        </aside>
    </div>

<?php include 'footer.php' ?>